package java101.Cheats;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class ArrayExpressions {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ArrayList<String> arrayList = new ArrayList(); //Array lists can grow forever. <String> specifies its type.
		arrayList.add("one"); 
		arrayList.add("two");
		arrayList.add("buckle my shoe");
		for (String s : arrayList) {
			System.out.println(s.toUpperCase());
		}
		
		System.out.println(arrayList.get(2)); //Calls the arraylist position 2.
		
		System.out.println(arrayList.contains("two")); //Checks for content - can only be done with arrayList
		
//		arrayList.remove(4); //Removes item four and bumps item 5 to item 4.
		
		Object[] array = arrayList.toArray(); //Turns the arrayList into a regular array.
		String[] array2 = arrayList.toArray(new String[]{}); //Feed it an empty array of certain type to determine the array type.
		Arrays.asList(array2); //Expensive for memory, but turns arrays back to arrayLists.
		
		System.out.println();
		System.out.println("---");
		System.out.println();
		
		TreeMap<String,Integer> wordCount = new TreeMap<String,Integer>(); //Two types in one array, similar to an arrayList since it resizes.
			try { //TreeMap keeps things in the order they are listed, as opposed to HashMap, which orders them as it chooses.
			FileReader fileReader = new FileReader("50states.txt");
			BufferedReader bufferedReader = new BufferedReader(fileReader); 
			String word = bufferedReader.readLine();
			
			while (word != null) {
				String firstLetter = word.substring(0, 1); //Looks at first letter
				Integer count = wordCount.get(firstLetter); //Gets the value associated with the first letter Returns null if no key is present.
				if (count != null) {
					wordCount.put(firstLetter, ++count);
				}
				else {
					wordCount.put(firstLetter, 1);
				}
				word = bufferedReader.readLine();
			}
			
			
			}
			catch (FileNotFoundException e) {
			System.out.println("The file could not be found.");
			} 
			
			catch (IOException e) {
			System.out.println("There is an error in the code.");
			}
			
		
			for (String key : wordCount.keySet()) { //"set" gets a set of objects which is the key.
				System.out.println(key + ":" + wordCount.get(key)); //Maps are not ordered. Keys return in any order, making a scrambled order.
			}
	}

}
	